//# sourceURL=http://uMapApp/Module/PQI/PQIStatistic
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum
) {

    function PQIStatistic(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.startStakeNum = StakeNum.transform(_this["开始桩号"]);
        _this.stopStakeNum = StakeNum.transform(_this["结束桩号"]);
        _this.currentLevelColor = "#fff";

        _this.colorAlphas = {
            normal  : 0.5,
            hover   : 0.5,
            active  : 1,
            polyline: 1
        };

        _this.directionCodeDic = {
            "U" : "上行",
            "D" : "下行",
            "UD": "双向"
        };

        _this["朝向"] = _this.directionCodeDic[_this.directionCode];


        if (_this["车道序号"]) {
            _this["车道"] = "第" + ["一", "二", "三"][_this["车道序号"] - 1] + "车道";
        } else {
            _this["车道"] = "整体";
        }

        _this.title = _this["朝向"] + " / " + _this["开始桩号"] + " - " + _this["结束桩号"] + " / " + _this["车道"];

        _this.searchKey = _this["朝向"] + " " + _this["开始桩号"] + " " + _this["结束桩号"] + " " + _this["车道"];

        _this.sortKey = (_this["朝向"] === "上行" ? 1e7 : 2e7) + _this.startStakeNum.distance + (_this["车道序号"] === undefined ? 0 : _this["车道序号"]) * 0.1;


        if (_this.positions) {

            _this.positions = new uMap.Positions(_this.positions);

            _this.layer = new Layer({
                "app"     : _this.app,
                "viewport": new ViewPort({
                    "center": _this.positions.positions[2],
                    "hpr"   : {
                        "heading": uMap.Positions.getHeading(_this.positions.positions[0], _this.positions.positions[2]),
                        "pitch"  : -20,
                        "range"  : 600
                    }
                })
            });

            _this.entity = new uMap.Entity({
                objectOption: {
                    polygon : new uMap.PolygonGraphic({
                        objectOption: {
                            positions: {positions: _this.positions}
                            , color  : [_this.currentLevelColor, _this.colorAlphas.normal]
                            // ,outlineColor: [_this.levelColor]
                            // ,extrudedHeight         : 1
                            // ,extrudedHeightReference: uEarth.HeightReference.RELATIVE_TO_GROUND
                        }
                    }),
                    polyline: new uMap.PolylineGraphic({
                        objectOption: {
                            positions    : _this.positions.positions.concat([_this.positions.positions[0]]),
                            clampToGround: true,
                            color        : [_this.currentLevelColor, _this.colorAlphas.polyline],
                            width        : 2
                        }
                    })
                }
            });

            _this.entity.on("click", function (event) {
                _this.trigger("click", event);
            });

            _this.layer.dataSources.entities.add(_this.entity.object);

        }


    }

    var _class = PQIStatistic;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"            : {
            value: function () {
                if (!this.isOpened) {
                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"           : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "remove"          : {
            value: function () {
                this.layer && this.layer.remove();
                delete this;
            }
        },
        "toggle"          : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active"          : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            // _this.layer.active();
                            // _this.polyline.show = false;
                            // _this.polygon.show = true;

                            _this.entity.polygon.material.color = new uMap.Color([_this.currentLevelColor, _this.colorAlphas.active]);

                        }

                        _this.trigger("active");

                    } else {

                        if (_this.layer) {
                            // _this.layer.close();
                            // _this.polyline.show = true;
                            // _this.polygon.show = false;

                            _this.entity.polygon.material.color = new uMap.Color([_this.currentLevelColor, _this.colorAlphas.normal]);

                        }

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }


            }
        },
        "getLevel"        : {
            value: function (param, value, standard) {
                var _this = this;

                var result;
                var currentLevels = _this.dic.params[param].levels;

                if (currentLevels) {
                    $.each(currentLevels, function (i, level) {

                        var currentLevel = level[standard] || level.GB;
                        var max = Number(currentLevel.split(",")[0]);
                        var min = Number(currentLevel.split(",")[1]);

                        if (value >= min && value < max) {
                            result = i;
                            return false;
                        }

                    });

                }

                return result;

            }
        },
        "standard"        : {
            set: function (standard) {
                var _this = this;
                _this._standard = standard;
                _this.updateLevel();
                _this.updateParamLevel();
            },
            get: function () {
                return this._standard;
            }
        },
        "currentParam"    : {
            set: function (param) {

                var _this = this;
                _this._currentParam = param;
                _this.updateParamLevel();
            },
            get: function () {
                return this._currentParam;
            }
        },
        "updateLevel"     : {
            value: function () {
                var _this = this;

                $.each(_this.dic.params, function (param, paramO) {

                    if (_this.listItem[param]) {
                        if (_this.param[param] !== undefined) {
                            var level = _this.getLevel(param, _this.param[param], _this.standard);
                            if (level && _this.dic.levels[level].className) {
                                _this.listItem[param].removeClass();
                                _this.listItem[param].addClass("level");
                                _this.listItem[param].addClass(_this.dic.levels[level].className);
                            }
                        }
                    }

                });

            }
        },
        "updateParamLevel": {
            value: function () {
                var _this = this;

                if (_this.param[_this._currentParam] !== undefined) {
                    _this.currentLevel = _this.getLevel(_this._currentParam, _this.param[_this._currentParam], _this.standard);

                    if (_this.currentLevel && _this.dic.levels[_this.currentLevel].color) {
                        _this.currentLevelColor = _this.dic.levels[_this.currentLevel].color;
                    }

                    if (_this.stakeNumNode) {

                        var GBLevel = _this.getLevel(_this._currentParam, _this.param[_this._currentParam], "GB");
                        var JSLevel = _this.getLevel(_this._currentParam, _this.param[_this._currentParam], "JS");

                        _this.stakeNumNode.JS.css({"background-color": _this.dic.levels[JSLevel].color});
                        _this.stakeNumNode.GB.css({"background-color": _this.dic.levels[GBLevel].color});
                    }

                } else {

                    _this.currentLevel = undefined;
                    _this.currentLevelColor = "#fff";

                    if (_this.stakeNumNode) {
                        _this.stakeNumNode.JS.removeAttr("style");
                        _this.stakeNumNode.GB.removeAttr("style");
                    }
                }

                _this.entity.polygon.material.color = new uMap.Color([_this.currentLevelColor, _this.isActive ? _this.colorAlphas.active : _this.colorAlphas.normal]);
                _this.entity.polyline.material.color = new uMap.Color([_this.currentLevelColor, _this.colorAlphas.polyline]);

                _this.trigger("update");
            }
        }
    });

    return _class;

});